package com.sjzsqjy.asdtiang.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjzsqjy.asdtiang.db.SearchService;
import com.sjzsqjy.asdtiang.mysql.TraceResult;
import com.sjzsqjy.asdtiang.mysql.TraceType;
import com.sjzsqjy.asdtiang.mysql.TraceUser;
import com.sjzsqjy.asdtiang.search.TraceResultFormat;

public class TraceSearchAction extends ActionSupport {

	private TraceUser patient;
	private List resultList;
	private String userId = "";
	private Log log = LogFactory.getLog(ShiQiaoSearchAction.class);

	private SearchService searchService;

	public String execute() throws Exception {
		List traceResultList;
		if (userId.equals("")) {
			patient = (TraceUser) ActionContext.getContext().getSession().get(
					"traceUser");
			traceResultList = searchService.getTraceUserListByUserId(patient
					.getId());
		} else {
			patient = searchService.getTraceUserById(userId);
			traceResultList = searchService.getTraceUserListByUserId(userId);
		}
		if(null==traceResultList){
			this.addFieldError("error","没有查找到相关数据");
			return ERROR;
		}
		resultList = new ArrayList();
		TraceResult it;
		TraceResultFormat t;
		TraceType type;
		for (int i = 0; i < traceResultList.size(); i++) {
			it = (TraceResult) traceResultList.get(i);
			t = new TraceResultFormat();
			type = searchService.getTraceTypeByName(it.getTypeId());
			t.setResult(it.getResult());
			t.setName(type.getName());
			t.setChild(type.getChild());
			t.setAdult(type.getAdult());
			t.setUnit(type.getUnit());
			t.setSequence(type.getSequence());
			resultList.add(t);
		}
		Collections.sort(resultList, new Comparator() {
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				return ((TraceResultFormat) o1).getSequence()
						- ((TraceResultFormat) o2).getSequence();
			}
		});
		return SUCCESS;
	}

	public TraceUser getPatient() {
		return patient;
	}

	public void setPatient(TraceUser patient) {
		this.patient = patient;
	}

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

}
